package com.itya.weatherback.domain.getWeatherAli.weatherJson;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
public class SevenDay {
    @JSONField(name = "time")
    private String time; // 日期描述，如"10日（周日）"

    // 新增字段：标准日期格式
    private LocalDate localDate;

    // 新增 setter 方法：自动解析 time 字段
    public void setTime(String time) {
        this.time = time;
        this.localDate = parseDate(time);
    }

    // 使用正则表达式提取日期数字
    private LocalDate parseDate(String time) {
        try {
            // 使用正则提取“X日”中的X
            Pattern pattern = Pattern.compile("(\\d+)日");
            Matcher matcher = pattern.matcher(time);
            if (matcher.find()) {
                int day = Integer.parseInt(matcher.group(1));

                // 获取当前日期
                LocalDate now = LocalDate.now();

                // 构建新的LocalDate（假设为当前年份+月份）
                return now.withDayOfMonth(day);
            }
        } catch (Exception e) {
            System.out.println("日期解析失败: " + time + "，原因：" + e.getMessage());
        }
        return null;
    }



    @JSONField(name = "weather")
    private String weather; // 天气描述，如"大雨转中雨"

    @JSONField(name = "temperature_max")
    private String temperatureMax;

    @JSONField(name = "temperature_min")
    private String temperatureMin;

    @JSONField(name = "day_weather_pic")
    private String dayWeatherPic;

    @JSONField(name = "night_weather_pic")
    private String nightWeatherPic;

    @JSONField(name = "wind_day")
    private String dayWind;

    @JSONField(name = "wind_night")
    private String nightWind;
}
